﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QRCoder;

namespace TestQrPrint
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap bitMap;
                Bitmap rotatedBitmap;
                QRCodeGenerator qrGenerator = new QRCodeGenerator();
                string text = "GEN5N-R 202508280014682C560 4682C561 4682C562 4682C560 4682C561\r\n4682C562 4682C560 4682C561 4682C562 4682C560\r\n" +
                    "4682C560 4682C561 4682C562 4682C560 4682C561\r\n4682C562 4682C560 4682C561 4682C562 4682C560";
//                

                QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
                QRCode qrCode = new QRCode(qrCodeData);
                Bitmap qrCodeImage = qrCode.GetGraphic(20); // 20x20像素的尺寸

                // 添加文字
                string text2 = "S250828004";
                using (Graphics graphics = Graphics.FromImage(qrCodeImage))
                {
                    bitMap = new Bitmap(qrCodeImage.Width, qrCodeImage.Height+220);
                    Graphics g = Graphics.FromImage(bitMap);
                    g.Clear(Color.White);
                    //画二维码
                    g.DrawImage(qrCodeImage, new Rectangle(0, 0, qrCodeImage.Width, qrCodeImage.Height));

                    //添加文字
                    Font font = new Font("Arial", 140, FontStyle.Bold);
                    SolidBrush sbrush = new SolidBrush(Color.Black); //设置颜色
                    g.DrawString(text2, font, sbrush, new PointF(150, qrCodeImage.Height-20));

                    rotatedBitmap = (Bitmap)bitMap.Clone();
                    rotatedBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    rotatedBitmap.Save(@"d:\qrcode_with_text.png", System.Drawing.Imaging.ImageFormat.Png);
                    pictureBox1.Image = rotatedBitmap;
                    
                }

                int dpi = 203;//300;
                Bitmap printBitmap = (Bitmap)rotatedBitmap.Clone();
                PrintDocument printDocument = new PrintDocument();
                printDocument.PrintPage += (sender1, args) => {
                    Graphics graphics = args.Graphics;
                    graphics.PageUnit = GraphicsUnit.Pixel; // Set unit to pixel for accurate scaling
                    float a4Width = 40 * dpi / 25.4f; // A4 width in pixels assuming 300 DPI
                    float a4Height = 30 * dpi / 25.4f; // A4 height in pixels assuming 300 DPI
                    float scaleWidth = a4Width / printBitmap.Width;
                    float scaleHeight = a4Height / printBitmap.Height;
                    float scale = Math.Min(scaleWidth, scaleHeight); // * 0.66f; // Use the smaller scale to fit within A4 size
                    graphics.DrawImage(printBitmap, new RectangleF(0, 0, printBitmap.Width * scale, printBitmap.Height * scale)); // Draw scaled image
                };
                printDocument.Print(); // Print the document with the scaled image on A4 size paper
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }
        static Bitmap RotateImage(Bitmap original, float angle)
        {
            // 创建一个空的位图，其大小足以包含旋转后的图像
            int width = original.Height;
            int height = original.Width;
            Bitmap rotatedBmp = new Bitmap(width, height);
            rotatedBmp.SetResolution(original.HorizontalResolution, original.VerticalResolution);

            // 创建一个Graphics对象用于绘制旋转的图像
            using (Graphics g = Graphics.FromImage(rotatedBmp))
            {
                // 设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                // 创建一个旋转矩阵
                // 注意：对于90度旋转，我们需要调整角度并使用不同的旋转点
                g.TranslateTransform((float)width, (float)height); // 移动到旋转点（图像的中心）
                g.RotateTransform(angle); // 旋转图像
                g.TranslateTransform((0 - width), (0 - height)); // 移动回原点

                // 绘制旋转后的图像到新的位图上
                g.DrawImage(original, new Point(0, 0));
            }




            //Bitmap originalBitmap = (Bitmap)bitMap.Clone();
            //rotatedBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height); // 创建一个新位图用于存储结果
            //rotatedBitmap.SetResolution(originalBitmap.HorizontalResolution, originalBitmap.VerticalResolution); // 设置分辨率以保持清晰度
            //using (Graphics g2 = Graphics.FromImage(rotatedBitmap))
            //{
            //    g2.TranslateTransform(originalBitmap.Width, 0); // 移动到图像的中心点（此处仅为示例，实际上不需要移动）
            //    g2.RotateTransform(90); // 旋转90度
            //    g2.TranslateTransform(0, 0); // 移动回原点（实际上不需要）因为上面的TranslateTransform已经足够将图像置于正确的位置进行绘制）
            //    g2.DrawImage(originalBitmap, new Point(0, 0)); // 绘制旋转后的图像到新位图上
            //}
            return rotatedBmp;
        }
    }
}
